package com.dzsw.showdoc.gen;

import com.dzsw.okhttp.OkHttp;
import com.dzsw.showdoc.bean.ResPage;
import com.dzsw.showdoc.bean.ResPageInfo;
import com.dzsw.showdoc.bean.ShowDocConf;
import com.dzsw.utils.DateUtil;
import com.dzsw.utils.JsonUtil;
import com.dzsw.utils.YamlUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * http://showdoc.fyrentcar.com:8088/server/index.php?s=/api/catalog/catListGroup
 * item_id: 16
 * {"error_code":0,"data":[{"cat_id":"125","cat_name":"\u77ed\u4fe1\u9879\u76ee\u63a5\u53e3","item_id":"16","s_number":"99","addtime":"2021-07-05 10:04:16","parent_cat_id":"0","level":"2","sub":[]},{"cat_id":"131","cat_name":"\u624b\u673a\u7248\u5927\u5c4f\u6570\u636e\u63a5\u53e3","item_id":"16","s_number":"99","addtime":"2021-08-13 08:58:42","parent_cat_id":"0","level":"2","sub":[]},{"cat_id":"132","cat_name":"OTA\u5bf9\u63a5\u63a5\u53e3","item_id":"16","s_number":"99","addtime":"2021-08-23 13:48:03","parent_cat_id":"0","level":"2","sub":[]},{"cat_id":"134","cat_name":"OTA\u5458\u5de5\u7aef\u63a5\u53e3","item_id":"16","s_number":"99","addtime":"2021-09-02 09:50:02","parent_cat_id":"0","level":"2","sub":[{"cat_id":"135","cat_name":"\u57ce\u5e02\u7ecf\u7406\u63a5\u53e3","item_id":"16","s_number":"99","addtime":"2021-09-02 09:50:49","parent_cat_id":"134","level":"3","sub":[]},{"cat_id":"136","cat_name":"\u51fa\u884c\u987e\u95ee\u63a5\u53e3","item_id":"16","s_number":"99","addtime":"2021-09-03 09:07:24","parent_cat_id":"134","level":"3","sub":[]}]}]}
 * <p>
 * http://showdoc.fyrentcar.com:8088/server/index.php?s=/api/catalog/getPagesBycat
 * item_id=16&cat_id=132
 * {"error_code":0,"data":[{"page_id":"1134","page_title":"\u63a5\u53e3\u8bf4\u660e","s_number":"1"},{"page_id":"1110","page_title":"\u540c\u6b65\u57ce\u5e02\u63a5\u53e3","s_number":"2"},{"page_id":"1116","page_title":"\u540c\u6b65\u95e8\u5e97\/\u914d\u9001\u70b9\u63a5\u53e3","s_number":"3"},{"page_id":"1149","page_title":" \u540c\u6b65\u8f66\u578b\u4fe1\u606f\uff08\u4ea7\u54c1\u4fe1\u606f\uff09","s_number":"4"},{"page_id":"1121","page_title":"\u4ea7\u54c1\u5217\u8868\u63a5\u53e3","s_number":"5"},{"page_id":"1132","page_title":"\u4ea7\u54c1\u8be6\u7ec6\u4fe1\u606f\u63a5\u53e3","s_number":"6"},{"page_id":"1148","page_title":"\u67e5\u8be2\u9884\u5b9a\u8f66\u8f86\u521d\u59cb\u8ba2\u5355\u4ef7\u683c\u660e\u7ec6","s_number":"7"},{"page_id":"1150","page_title":"\u9a8c\u8bc1\u4ea7\u54c1\u6392\u671f\u63a5\u53e3","s_number":"8"},{"page_id":"1131","page_title":"\u521b\u5efa\u8ba2\u5355\u63a5\u53e3","s_number":"9"},{"page_id":"1133","page_title":"\u67e5\u8be2\u8ba2\u5355\u4fe1\u606f","s_number":"10"},{"page_id":"1125","page_title":"\u53d6\u6d88\u8ba2\u5355\u9a8c\u8bc1\u63a5\u53e3","s_number":"11"},{"page_id":"1124","page_title":"\u8fd8\u8f66\u63a5\u53e3","s_number":"12"},{"page_id":"1123","page_title":"\u8ba2\u5355\u5b8c\u6210\u63a5\u53e3","s_number":"13"},{"page_id":"1151","page_title":"\u56de\u8c03\u63a5\u53e3\uff08\u4e0b\u5355\u7ed3\u679c\u901a\u77e5\uff09","s_number":"99"},{"page_id":"1152","page_title":"\u56de\u8c03\u63a5\u53e3\uff08\u8ba2\u5355\u53d8\u5316\u901a\u77e5\uff09","s_number":"99"},{"page_id":"1165","page_title":"\u54cd\u5e94\u72b6\u6001","s_number":"99"}]}
 * <p>
 * http://showdoc.fyrentcar.com:8088/server/index.php?s=/api/page/info
 * page_id=1150
 * {"error_code":0,"data":{"page_id":"1150","author_uid":"40","author_username":"sunzhe","item_id":"16","cat_id":"132","page_title":"\u9a8c\u8bc1\u4ea7\u54c1\u6392\u671f\u63a5\u53e3","page_content":"\n### \u9a8c\u8bc1\u4ea7\u54c1\u6392\u671f\u63a5\u53e3\n\n**\u63a5\u53e3\u63cf\u8ff0 **\n- \u9a8c\u8bc1\u4ea7\u54c1\u6392\u671f\u3001\u4f18\u60e0\u5238\u662f\u5426\u53ef\u7528\u3002\n\n**\u63a5\u53e3\u5730\u5740 **\n- ` \/fyapi\/zuche\/order\/check`\n\n**\u8bf7\u6c42\u65b9\u5f0f **\n- POST\n\n**\u8bf7\u6c42\u53c2\u6570 **\n\n|\u53c2\u6570\u540d|\u5fc5\u9009|\u7c7b\u578b|\u8bf4\u660e|\n|:----    |:---|:----- |-----   |\n|prod_id |\u5426  |int |\u4ea7\u54c1Id|\n|start_time |\u5426 |sting |\u9884\u8ba1\u7528\u8f66\u65f6\u95f4-\u5f00\u59cb\uff0c\u683c\u5f0f\uff1ayyyy-MM-dd HH:mm:ss\u3002 \u6b64\u53c2\u6570\u5fc5\u987b\u540c\u4ea7\u54c1Id\u3001end_time\u4e00\u8d77\u4f7f\u7528\u3002|\n|end_time |\u5426  |sting |\u9884\u8ba1\u7528\u8f66\u65f6\u95f4-\u7ed3\u675f\uff0c\u683c\u5f0f\uff1ayyyy-MM-dd HH:mm:ss\u3002 \u6b64\u53c2\u6570\u5fc5\u987b\u540c\u4ea7\u54c1Id\u3001start_time\u4e00\u8d77\u4f7f\u7528\u3002|\n|coupon_code |\u5426  |int |\u4f18\u60e0\u5377\u7801|\n\n **\u54cd\u5e94\u72b6\u6001 **\n\n|\u7f16\u7801|\u9519\u8bef\u6d88\u606f|\u5907\u6ce8|\n|:-----|:-----| -----|\n|200 |\u8c03\u7528\u6210\u529f\u3002| |\n|500 |\u8c03\u7528\u5931\u8d25\u3002| |\n\n**\u54cd\u5e94\u53c2\u6570 **\n\n|\u53c2\u6570\u540d|\u5fc5\u9009|\u7c7b\u578b|\u8bf4\u660e|\n|:----    |:---|:----- |-----   |\n| code|\u662f|int |\u54cd\u5e94\u72b6\u6001\uff0c\u8be6\u89c1\u201c\u54cd\u5e94\u72b6\u6001\u201d\u3002|\n| total|\u662f|int |\u5206\u9875\u67e5\u8be2\u603b\u9875\u6570\uff0c\u5982\u65e0\u5206\u9875\u4e3a0\u3002|\n| message|\u662f|string |\u8fd4\u56de\u6d88\u606f\u3002|\n| data|\u662f| object |\u54cd\u5e94\u6570\u636e\u3002|\n\n**data\u53c2\u6570\u8bf4\u660e**\n\n|\u53c2\u6570\u540d|\u5fc5\u9009|\u7c7b\u578b|\u8bf4\u660e|\n|:----    |:---|:----- |-----   |\n| prod_enable|\u662f|int |\u4ea7\u54c1\u662f\u5426\u53ef\u7528\u3002 <br\/> 0=\u4e0d\u53ef\u7528 <br\/> 1=\u53ef\u7528|\n| schedule_enable|\u662f|int |\u6392\u671f\u662f\u5426\u53ef\u7528\uff0c\u5982\u679c\u672a\u8f93\u5165\u6392\u671f\u5219\u8fd4\u56de0\u3002 <br\/> 0=\u4e0d\u53ef\u7528 <br\/> 1=\u53ef\u7528|\n| coupon_enable|\u662f|int |\u4f18\u60e0\u5238\u662f\u5426\u53ef\u7528\u3002<br\/> 0=\u4e0d\u53ef\u7528 <br\/> 1=\u53ef\u7528|\n\n\n **\u6210\u529f\u793a\u4f8b**\n\n```\n{\n    \"code\": 200,             \/\/ \u6d88\u606f\u7f16\u7801\uff0c200=\u8bf7\u6c42\u6210\u529f\n    \"data\": {                \/\/ \u8fd4\u56de\u6570\u636e\n        [\n            {\n\t\t\t\t\"prod_enable\": 1,\n\t\t\t\t\"schedule_enable\": 1,\n\t\t\t\t\"coupon_enable\": 1,\n            }\n        ]\n    },\n    \"total\": 0,              \/\/ \u603b\u9875\u6570\n    \"message\": \"\u8bbf\u95ee\u6210\u529f\u3002\"   \/\/ \u6d88\u606f\n}\n```\n\n **\u5931\u8d25\u793a\u4f8b**\n```\n{\n    \"code\": 500,             \/\/ \u6d88\u606f\u7f16\u7801\uff0c200=\u8bf7\u6c42\u6210\u529f\n    \"data\": null,\n    \"total\": 0,              \/\/ \u603b\u9875\u6570\n    \"message\": \"\u8bbf\u95ee\u5931\u8d25\u3002\"   \/\/ \u6d88\u606f\n}\n```\n\n","s_number":"8","addtime":"2021-09-02 15:16:34","page_comments":"","is_del":"0","attachment_count":"0","unique_key":""}}
 * <p>
 * Cookie: cookie_token=a701b50ddeba09a57c5a7a9c90bd7db2666ca644a7e6d16bd98cce4eb805d726; PHPSESSID=ae5c57d41994df0477be215c52bccf11; think_language=zh-CN
 */
public class DocGenerator {


    public static void main(String[] args) throws Exception {
        ShowDocConf conf = YamlUtil.readYaml("showdoc.yml", ShowDocConf.class);
        System.out.println(conf.getGetPagesBycatUrl());
        // 创建Okhttp
        OkHttp okHttp = new OkHttp();
        okHttp.build();
        // 访问列表获取Cat列表
        Map<String, String> header = new HashMap<>();
        header.put("Cookie", conf.getCookieValue());
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("item_id", conf.getItemId());
        paramMap.put("cat_id", conf.getCatId());
        String returnStr = okHttp.postMapObject(conf.getGetPagesBycatUrl(), paramMap, header);
        Map<String, Object> returnMap = JsonUtil.jsonToMap(returnStr);
        List<Map<String, Object>> pageList = (List<Map<String, Object>>) returnMap.get("data");
        // 循环访问并写入文件
        FileOutputStream fos = null;
        try {
            String savePath = conf.getSavePath();
            File sp = new File(savePath);
            if (!sp.exists()) {
                sp.mkdirs();
            }
            String fileName = DateUtil.formatNow("yyyy_MM_dd_HH_mm_ss") + ".md";
            fos = new FileOutputStream(conf.getSavePath() + fileName);
            for (Map<String, Object> p : pageList) {
                ResPage resPage = new ResPage();
                resPage.putAll(p);
                paramMap = new HashMap<>();
                paramMap.put("page_id", resPage.getPageId());
                returnStr = okHttp.postMapObject(conf.getPageInfoUrl(), paramMap, header);
                returnMap = JsonUtil.jsonToMap(returnStr);
                Map<String, Object> dataMap = (Map<String, Object>) returnMap.get("data");
                ResPageInfo pi = new ResPageInfo();
                pi.putAll(dataMap);
                String content = pi.getContent();
                content += "\n------\n";
                fos.write(content.getBytes(StandardCharsets.UTF_8));
            }
            fos.flush();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (fos != null) {
                fos.close();
            }
        }
    }
}
